package com.dynamic.mybatis.core.parser;

import java.util.Objects;
import java.util.UUID;

public class NamespaceHelper {
    public  final static String DEFAULT_NAMESPACE = "dms";
    public static String getDynamicMappedStatementId(String namespace,String shortId){
         namespace = getNamespace(namespace);
         shortId = Objects.toString(shortId,"");
         if(shortId.startsWith(namespace)){ return shortId; }
         return namespace.endsWith(".") ? namespace+shortId : namespace+"."+shortId;
    }

    public static String getRandomNamespace(){
       return UUID.randomUUID().toString().replaceAll("-","")+"."+UUID.randomUUID().toString().replaceAll("-","");
    }

    public static String getNamespace(String namespace){
        if(namespace == null || namespace.trim().length() < 1){
            return DEFAULT_NAMESPACE;
        }
        return namespace;
    }

    public static String getIdentity(String namespace,String id){
        namespace = getNamespace(namespace);
        return namespace.endsWith(".") ? namespace+id : namespace+"."+id;
    }
}
